package com.campus.admin.controller;

import cn.hutool.core.date.DateTime;
import com.campus.common.core.domain.AjaxResult;
import com.campus.pojos.domain.BillBoard;
import com.campus.pojos.domain.dto.BillBoardDto;
import com.campus.pojos.domain.dto.BillBoardQueryDto;
import com.campus.pojos.domain.vo.BillBoardVo;
import com.campus.service.BillboardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 公告栏相关接口
 */
@RestController(value = "adminBillboardController")
@RequestMapping("/system/admin/billboard")
@Slf4j
public class BillboardController {
    @Autowired
    BillboardService billboardService;
    /**
     * 编辑公告栏
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody BillBoardDto dto){
        log.info("编辑公告,{}",dto);
        billboardService.edit(dto);
        return AjaxResult.success();
    }

    /**
     * 公告栏条件查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public AjaxResult getList(@RequestBody BillBoardQueryDto dto){
        return billboardService.getList(dto);
    }

    /**
     * 根据id获取公告
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id){
        if(id == null) return AjaxResult.error("公告栏id为空");
        BillBoard billBoard = billboardService.getById(id);
        BillBoardVo billBoardVo = new BillBoardVo();
        BeanUtils.copyProperties(billBoard, billBoardVo);
        billBoardVo.setShow(billBoard.getShow() ? 1 : 0);
        return AjaxResult.success("success", billBoardVo);
    }


    /**
     * 根据id删除公告
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        if(id == null) return AjaxResult.error("公告栏id为空");
        return AjaxResult.success("success", billboardService.removeById(id));
    }

    /**
     * 根据id获取公告
     * @param id
     * @return
     */
    @PutMapping("/show")
    public AjaxResult getById(
            @RequestParam(value = "id", required = true) Integer id,
            @RequestParam(value = "show", required = true) Integer show
    ){
        if(id == null) return AjaxResult.error("公告栏id为空");
        return billboardService.status(id, show);
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody BillBoardDto dto){
        billboardService.saveBillboard(dto);


        return AjaxResult.success();
    }

    /**
     * 根据ids删除公告
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public AjaxResult deleteById(@RequestParam(value = "ids",required = false) List<Integer> ids){
        billboardService.removeByIds(ids);
        return AjaxResult.success();
    }

}
